using System.Collections.Generic;
using System.Linq;
using SurveySez.Model;
using System.Windows.Input;
using UpdateControls.XAML;
using System;
using SurveySez.NavigationModel;

namespace SurveySez.ViewModel.Editor
{
	public class GameDetailsViewModel
	{
		private EmceeGame _emceeGame;
		private EditorNavigationModel _navigation;

		public GameDetailsViewModel(EmceeGame emceeGame, EditorNavigationModel navigation)
		{
			_emceeGame = emceeGame;
			_navigation = navigation;
		}

		public string Name
		{
			get { return _emceeGame.Game.Name; }
			set { _emceeGame.Game.Name = value; }
		}

		public IEnumerable<GameSurveyHeaderViewModel> Surveys
		{
			get
			{
				return _emceeGame.Game.GameSurveys
					.OrderBy(gameSurvey => gameSurvey.Order)
					.Select(gameSurvey => new GameSurveyHeaderViewModel(gameSurvey));
			}
		}

		public GameSurveyHeaderViewModel SelectedSurvey
		{
			get
            {
            	return _navigation.SelectedGameSurvey == null
					? null
					: new GameSurveyHeaderViewModel(_navigation.SelectedGameSurvey);
            }
			set
            {
            	_navigation.SelectedGameSurvey = value == null
					? null
					: value.GameSurvey;
            }
		}

		public ICommand AddSurvey
		{
			get
			{
				return MakeCommand
					.When(() => _navigation.SelectedSurvey != null)
					.Do(() => _navigation.SelectedGameSurvey = _emceeGame.Game.AddSurvey(_navigation.SelectedSurvey.Survey));
			}
		}

		public ICommand RemoveSurvey
		{
			get
			{
				return MakeCommand
					.When(() => _navigation.SelectedGameSurvey != null)
					.Do(() => _navigation.SelectedGameSurvey.Delete());
			}
		}

		public ICommand MoveSurveyUp
		{
			get
			{
				return MakeCommand
					.When(() =>
						_navigation.SelectedGameSurvey != null &&
						_emceeGame.Game.GameSurveys.Any(survey => survey.Order < _navigation.SelectedGameSurvey.Order))
					.Do(() =>
					{
						float priorOrder = _emceeGame.Game.GameSurveys
							.Where(survey => survey.Order < _navigation.SelectedGameSurvey.Order)
							.Max(survey => survey.Order);
						float earlierOrder = _emceeGame.Game.GameSurveys
							.Where(survey => survey.Order < priorOrder)
							.Max(survey => (float?)survey.Order) ?? 0.0f;

						Survey selectedSurvey = _navigation.SelectedGameSurvey.Survey;
						_navigation.SelectedGameSurvey.Delete();
                        _navigation.SelectedGameSurvey = _emceeGame.Game.InsertSurveyAt(selectedSurvey, (priorOrder + earlierOrder) / 2.0f);
					});
			}
		}

		public ICommand MoveSurveyDown
		{
			get
			{
				return MakeCommand
					.When(() =>
						_navigation.SelectedGameSurvey != null &&
						_emceeGame.Game.GameSurveys.Any(survey => survey.Order > _navigation.SelectedGameSurvey.Order))
					.Do(() =>
					{
						float nextOrder = _emceeGame.Game.GameSurveys
							.Where(survey => survey.Order > _navigation.SelectedGameSurvey.Order)
							.Min(survey => survey.Order);
						float laterOrder = _emceeGame.Game.GameSurveys
							.Where(survey => survey.Order > nextOrder)
							.Min(survey => (float?)survey.Order) ?? nextOrder + 2.0f;

						Survey selectedSurvey = _navigation.SelectedGameSurvey.Survey;
						_navigation.SelectedGameSurvey.Delete();
                        _navigation.SelectedGameSurvey = _emceeGame.Game.InsertSurveyAt(selectedSurvey, (nextOrder + laterOrder) / 2.0f);
					});
			}
		}
	}
}
